package com.yyt.module_main.act;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;
import com.yyt.libsrc.bean.BaseCallbackBean;
import com.yyt.libsrc.bean.SubordinateListBean;
import com.yyt.libsrc.bean.SubordinateStatisticsBean;
import com.yyt.libsrc.bean.SubordinatebyListBean;
import com.yyt.libsrc.bean.TotalpayBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityMemberManageBinding;
import com.yyt.module_main.databinding.ItemMembermanageSimpleBinding;
import com.yyt.module_main.vm.MemberManageVM;

import java.util.List;

import top.lixb.libcommon.base.ItemHolder;
import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.base.SimpleBindingAdapter;
import top.lixb.libcommon.helper.GlideHelper;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_member_manage)
public class MemberManageActivity extends RefreshableActivity<ActivityMemberManageBinding> {

    private MemberManageVM mVm;
    private SimpleBindingAdapter<SubordinatebyListBean.DataBean.ListBean, ItemMembermanageSimpleBinding> mAdapter;

    @Override
    protected void init() {
        mVm = new MemberManageVM();
        mRefreshContentBinding.setVm(mVm);
        setNoTitle();
        mStatusbar.setVisibility(View.GONE);
        mRefreshContentBinding.rv.setLayoutManager(new GridLayoutManager(this,4));
        mAdapter = new SimpleBindingAdapter<SubordinatebyListBean.DataBean.ListBean, ItemMembermanageSimpleBinding>(R.layout.item_membermanage_simple) {
            @Override
            public void bind(@NonNull ItemHolder<ItemMembermanageSimpleBinding> holder, SubordinatebyListBean.DataBean.ListBean bean) {
                GlideHelper.loadImg(holder.getBinding().ivHeadIcon, bean.head_img);
                holder.getBinding().tvName.setText(bean.real_name);
            }
        };


        DefaultItemDecoration decor = new DefaultItemDecoration(Color.TRANSPARENT,5,5,-1);
        mRefreshContentBinding.rv.addItemDecoration(decor);
        mRefreshContentBinding.rv.setAdapter(mAdapter);
        mRefreshContentBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_member_manage;
    }

    @Override
    protected void getData(boolean refresh) {
        showLoading();
        getSubstatistics();
    }

    private void getSubstatistics() {

        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.totalPay)
                .build(), new SuccessCallback<TotalpayBean>() {
            @Override
            public void onSuccess(TotalpayBean totalpayBean) {
                mVm.teamIncom.set(StringUtils.doubleFormat(totalpayBean.data.totalpay));
                getTop10();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                finishGetData();
            }
        });

    }

//    private void getSubordinateList() {
//        NetUtil.sellerPost(new JavaParamsBuilder()
//                .addCmd(Consts.CMD.subordinateList)
//                .addP("type", String.valueOf(mVm.type.get()))
//                .addP("phoneOrName", mVm.phoneOrName.get())
//                .addP("startTime", String.valueOf(mVm.startTime.get()))
//                .addP("endTime", String.valueOf(mVm.endTime.get()))
//                .build(), new SuccessCallback<SubordinateListBean>() {
//            @Override
//            public void onSuccess(SubordinateListBean subordinateListBean) {
//                mAdapter.setNewData(subordinateListBean.data.list);
//                hideLoading();
//                finishGetData();
//            }
//
//            @Override
//            public void onComplete() {
//                super.onComplete();
//                hideLoading();
//                finishGetData();
//            }
//        });
//
//
//
//    }

    private void getTop10() {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.subordinatebylist)
                .addP("type","2")
                .build(), new SuccessCallback<SubordinatebyListBean>() {
            @Override
            public void onSuccess(SubordinatebyListBean bean) {
                List<SubordinatebyListBean.DataBean.ListBean> list = bean.data.list;
                if (list.size() > 8) {
                    list = list.subList(0, 8);
                }
                mAdapter.setNewData(list);
            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
            }
        });
    }
}
